package com.zd.attgen.service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Service;

import com.zd.attgen.entity.Attendance;
import com.zd.attgen.entity.Attendees;
import com.zd.attgen.entity.WorkTime;
import com.zd.attgen.entity.WorkTime.CheckinType;

/**
 * 生成器
 * @author zhang
 *
 */
@Service
public class GeneratorService {

	public List<Attendance> generate(LocalDate startLocalDate, LocalDate endLocalDate, 
			LocalTime openLocalTime, LocalTime closeLocalTime, List<LocalDate> workDayList, List<LocalDate> holidayList,
			List<String> attList, List<String> btAttList, List<String> leaveAttList, List<String> feedAttList) {
		List<Attendance> attendances = new ArrayList<Attendance>(2000);
		//获取考勤时间差
		long daysDiff = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
		
		//生成考勤数据
		for (String att : attList) {
			LocalTime openLocalTimeTmp = openLocalTime;
			LocalTime closeLocalTimeTmp = closeLocalTime;
			for (int i = 0; i <= daysDiff; i++) {
				//设置出勤人员
				Attendees attendees = new Attendees();
				attendees.setCnname(att);
				//设置工作时间
				WorkTime workTime = new WorkTime();
				workTime.setDate(startLocalDate.plusDays(i));
				workTime.setOpenTime(openLocalTime);
				workTime.setCloseTime(closeLocalTime);
				workTime.setCheckinType(CheckinType.normal);
//				System.out.println(workTime.getDate().getDayOfWeek() + " " + workTime.getDate().getDayOfWeek().getValue());
				int dayOfWeek = workTime.getDate().getDayOfWeek().getValue();				
				if(dayOfWeek == 6 || dayOfWeek == 7) {
					if(!workDayList.contains(workTime.getDate())) {
						workTime.setCheckinType(CheckinType.weekend);
					} else if(btAttList.contains(attendees.getCnname())) {
						workTime.setCheckinType(CheckinType.businessTravel);
					}
				} else if(holidayList.contains(workTime.getDate())) {
					workTime.setCheckinType(CheckinType.holiday);
				} else if(btAttList.contains(attendees.getCnname())) {
					workTime.setCheckinType(CheckinType.businessTravel);
				} else if(leaveAttList.contains(attendees.getCnname())) {
					workTime.setCheckinType(CheckinType.leave);
				} else if(feedAttList.contains(attendees.getCnname())) {
					workTime.setCheckinType(CheckinType.feed);
					//openLocalTimeTmp = openLocalTime.plusHours(-1);
					closeLocalTimeTmp = closeLocalTime.plusHours(-1);
				}
				//设置出勤
				Attendance attendance = new Attendance();
				attendance.setAttendees(attendees);
				attendance.setWorkTime(workTime);
				//随机生成出勤时间
				attendance.setCheckinTime(generateCheckinTime(openLocalTimeTmp));
				attendance.setSignoffTime(generateSignoffTime(closeLocalTimeTmp));
				//加入出勤集合
				attendances.add(attendance);
//				System.out.println(att + " " + workTime.getDate() + " " + workTime.getOpenTime() + " " + workTime.getCloseTime() + " " + attendance.getCheckinTime() + " " + attendance.getSignoffTime());
			}	
		}
		
		return attendances;
	}
	
	/**
	 * 生成签到时间
	 * @return
	 */
	private LocalTime generateCheckinTime(LocalTime openLocalTime) {
		//只能比上班时间提前，只能比下班时间靠后
		
		//签到时间前10分钟的，生成700条
		//签到时间前20分钟的，生成250条，
		//签到时间前30分钟的，生成40条，
		//签到时间前60分钟的，生成10条。
		List<LocalTime> times = new ArrayList<LocalTime>(1000);
		//在生成的1000条中取出1条
		Random r = new Random();
		for (int i = 0; i < 1000; i++) {
			int minutesToAdd;
			if(i < 700) {
				minutesToAdd = -r.nextInt(10);
			} else if( i < 950) {
				minutesToAdd = -r.nextInt(10) - 10;
			} else if ( i < 990) {
				minutesToAdd = -r.nextInt(10) - 20;
			} else {
				//待完善
				minutesToAdd = -r.nextInt(60);
			}
			times.add(openLocalTime.plusMinutes(minutesToAdd));
		}
		LocalTime newOpenLocalTime = times.get(r.nextInt(times.size()));
		return newOpenLocalTime;
	}

	//生成签退时间
	private LocalTime generateSignoffTime(LocalTime closeLocalTime) {
		List<LocalTime> times = new ArrayList<LocalTime>(1000);
		//签退时间
		Random r = new Random();
		for (int i = 0; i < 1000; i++) {
			int minutesToAdd;
			if(i < 600) {
				minutesToAdd = r.nextInt(10) + 1;
			} else if( i < 800) {
				minutesToAdd = r.nextInt(10) + 10;
			} else if ( i < 950) {
				minutesToAdd = r.nextInt(10) + 20;
			} else {
				//待完善
				minutesToAdd = r.nextInt(60);
			}
			times.add(closeLocalTime.plusMinutes(minutesToAdd));
		}
		LocalTime newCloseLocalTime = times.get(r.nextInt(times.size()));
		return newCloseLocalTime;
	}
}
